package com.zzt.service;

import com.zzt.dao.GwcMapper;
import com.zzt.entity.Gwc;
import com.zzt.entity.GwcExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class GwcServiceImpl implements GwcService{
    @Resource
    GwcMapper gwcMapper;

    //展示所有购物车商品
    @Override
    public List<Gwc> showAllGwc(int mno) {
        return gwcMapper.selectAllByMno(mno);
    }

    @Override
    public void addOneGoods(int gwcId) {
        Gwc gwc = gwcMapper.selectByPrimaryKey(gwcId);
        gwc.setNum(gwc.getNum()+1); //+1  然后更新
        gwcMapper.updateByPrimaryKey(gwc);
    }

    @Override
    public void subOneGoods(int gwcId) {
        Gwc gwc = gwcMapper.selectByPrimaryKey(gwcId);
        if(gwc.getNum()==1){   //如果只有1个，直接删掉
            gwcMapper.deleteByPrimaryKey(gwcId);
        }else {
            gwc.setNum(gwc.getNum()-1); //-1  然后更新
            gwcMapper.updateByPrimaryKey(gwc);
        }
    }

    @Override
    public void delGoods(int gwcId) {
        gwcMapper.deleteByPrimaryKey(gwcId);
    }

    @Override
    public void clearGwc(int mno) {
        GwcExample example = new GwcExample();
        GwcExample.Criteria criteria = example.createCriteria();
        criteria.andMnoEqualTo(mno);
        gwcMapper.deleteByExample(example);
    }

    @Override
    public int countGoods(int mno) {
        GwcExample example = new GwcExample();
        GwcExample.Criteria criteria = example.createCriteria();
        criteria.andMnoEqualTo(mno);
        int count = (int)gwcMapper.countByExample(example);
        return count;
    }
}
